<?php

/**
 * @author      :   NhanVA
 * @name        :   Fpt_Business_Match_Adapter_MySql
 * @copyright   :   Fpt Online
 * @todo        :   Business Match
 */
class Fpt_Business_Match_Adapter_MySql extends Fpt_Business_Match_Adapter_Abstract
{

    /**
     * Get singleton instance
     * @return Fpt_Business_Match_Adapter_MySql 
     */
    public static function getInstance ()
    {
        if (!isset(self::$_instance) || !self::$_instance instanceof self)
            self::$_instance = new self;
        return self::$_instance;
    }

    /**
     * Get hot news
     * @param array $arrParams
     * @return array
     * @author  : nhanva@fpt.net
     */
    public function getHotMatch ($strPosition, $intCategory)
    {
        $arrResult = array ();
        try
        {
            $sport_m = Fpt_Global::getSportDB();
            $stmt    = $sport_m->prepare('CALL sp_fesport_getHotMatch(:p_showedposition, :p_categoryid);');

            //Bind param
            $stmt->bindParam('p_showedposition', $strPosition, PDO::PARAM_STR);
            $stmt->bindParam('p_categoryid', $intCategory, PDO::PARAM_INT);

            //Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetch();

            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $arrResult;
    }

    /**
     * Get matches from begin date to end date
     * @param int $seasonId
     * @param int $leagueId
     * @param int $beginDate
     * @param int $endDate 
     * @return array|bool
     */
    public function getMatchInWeek ($seasonId, $leagueId, $beginDate, $endDate)
    {
        try
        {
            // Get DB instance
            $objDB = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $objDB->prepare('Call sp_code_sport_getMathScoreByTime(:p_season_id, :p_league_id, :p_start, :p_end);');

            // Bind params
            $stmt->bindParam('p_season_id', $seasonId, PDO::PARAM_INT);
            $stmt->bindParam('p_league_id', $leagueId, PDO::PARAM_INT);
            $stmt->bindParam('p_start', $beginDate, PDO::PARAM_INT);
            $stmt->bindParam('p_end', $endDate, PDO::PARAM_INT);

            // Excute
            $stmt->execute();

            // Fetch to get result
            $result = array ();
            $matchFormat = new Fpt_Business_Match_Metadata_Match();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                array_push($result, $matchFormat->init($row)->getFormatedData());
            }

            // Release variables
            unset($stmt, $matchFormat);

            // Return result
            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
    }

    /**
     * Get detail match by list ids
     * @param array $arrMatchIDs
     * @return array|boolean
     * @author QuangTM
     */
    public function getMatchDetailByIDs ($arrMatchIDs)
    {
        try
        {
            // Convert array ids to string
            $strMatchIDs = implode(',', $arrMatchIDs);

            // Get DB instance
            $objDB = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $objDB->prepare('Call sp_fesport_getDetailMatchByIDs(:p_matchids);');

            // Bind params
            $stmt->bindParam('p_matchids', $strMatchIDs, PDO::PARAM_STR);

            // Excute
            $stmt->execute();

            // Fetch to get result
            $result = array ();
            $matchFormat = new Fpt_Business_Match_Metadata_Match();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                array_push($result, $matchFormat->init($row)->getFormatedData());
            }

            // Release variables
            unset($stmt, $matchFormat, $strMatchIDs);

            // Return result
            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Get list match ids by happen time
     * @param int $begin
     * @param int $end
     * @return array|boolean 
     * @author QuangTM
     */
    public function getListMatchesByHappenTime ($begin, $end, $leagueID = NULL)
    {
        try
        {
            $begin -= 25200; 
            $end -= 25200;
            // Get DB instance
            $objDB = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $objDB->prepare('Call sp_fesport_getMatchByHappenTime(:p_begindate, :p_enddate, :p_league_id);');

            // Bind params
            $stmt->bindParam('p_begindate', $begin, PDO::PARAM_INT);
            $stmt->bindParam('p_enddate', $end, PDO::PARAM_INT);
            if(empty($leagueID))
                $stmt->bindParam('p_league_id', $leagueID, PDO::PARAM_NULL);
            else
                $stmt->bindParam('p_league_id', $leagueID, PDO::PARAM_INT);

            // Excute
            $stmt->execute();

            // Fetch to get result
            $result = array();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                array_push($result, $row['match_id']);
            }

            // Release variables
            unset($stmt);

            // Return result
            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }
     public function getMatchIDsBySeasonAndLeague($seasonID,$leagueID)
    {
        try
        {
            $sport_m = Fpt_Global::getSportSlaveDB();
            
            $stmt = $sport_m->prepare('CALL sp_besport_getMatchBySeason(:p_season_id, :p_league_id)');

            //Bind param
            $stmt->bindParam('p_season_id', $seasonID, PDO::PARAM_INT);
            $stmt->bindParam('p_league_id', $leagueID, PDO::PARAM_INT);
            
            //Fetch result
            $stmt->execute();
            $result = array();
            
            while ($row = $stmt->fetch()) {
                array_push($result, $row['match_id']);
            }
            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
            
            return $result;
        }
        catch (Zend_Db_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4,$ex->getCode(),$ex->getMessage(),$ex->getTraceAsString(),null, null);
            return false;
        }
        
    }

}